package com.zhku.meeting.util;

import cn.hutool.json.JSONUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Description: //TODO
 * Author: xiheng.li
 * Version: 1.0
 * Create at: 2023/3/9 13:46
 **/
public class ResultUtil {
    /**
     * Description: 返回未登录的错误信息
     * Method: returnNoLogin
     * Params: [response, msg]
     * Return: void
     * Date: 2023/3/9
     **/
    public static <T> void returnNoLogin(HttpServletResponse response, int status, String msg, T data) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        // 设置返回code 和响应编码
        response.setStatus(status);
        response.setContentType("Application/json;charset=utf-8");
        // 构造返回响应体
        ResultVO<T> result = ResultVO.create(String.valueOf(status),msg,data);
        String resultString = JSONUtil.toJsonStr(result);
        outputStream.write(resultString.getBytes(StandardCharsets.UTF_8));
    }
}